onload = function() {
	daohanglan();
	detail();
	memory();
	color();
	img();
	xiaImg();
	islike();

	document.querySelector("#search").addEventListener("keydown",keyboard)
	document.querySelector("#search").addEventListener("input",searchtips)
	document.querySelector(".searchtip").addEventListener("mouseleave",function (){
		document.querySelector(".searchtip").style.display="none"
	})

	document.querySelectorAll(".xlli").forEach(x=>{
		x.addEventListener("mouseover",xllijdbc);
	})
	a =document.querySelector(".showNavtwoson").innerHTML;
	document.querySelector(".newtuijian").addEventListener("mouseover",function (){
		document.querySelector(".showNavtwoson").innerHTML=a;
	})
	document.querySelectorAll(".gnb_nav_center").forEach(x=>{
		x.addEventListener("mouseover",function (){
			document.querySelector(".showNavtwoson").innerHTML=a;
		})
	})

	document.querySelectorAll(".xlli").forEach(x=>{
		x.addEventListener("click",function (){
			let xid = parseInt(event.target.getAttribute("id"));
			location.href="/goodsList?xid="+xid+"&show="+1;
		})
	})


//	加减
}

function keyboard(){
	let keycode = event.keyCode;
	if (keycode == 13){
		search();
	}
}

function search(){
	let searchvalue = document.querySelector("#search").value;
	if(searchvalue==""){
		event.preventDefault();
	}else {
		location.href="/index?v=search&searchvalue="+searchvalue;
	}

}

function searchtips(){
	if (document.querySelector("#search").value==""){
		document.querySelector(".searchtip").style.display="none"
	}else {
		document.querySelector(".searchtip").style.display="block"
	}
	let searchvalue = document.querySelector("#search").value;
	$.ajax({
		url:"/index?v=searchtip",
		type:"post",
		data:{
			searchvalue
		},
		dataType:"json",
		success:(resp=>{
			if (resp.length>4){
				document.querySelector(".searchtip").style.height="80px";
				document.querySelector(".searchtip").style.overflowY="scroll";
			}else if (resp.length==0){
				document.querySelector(".searchtip").style.height="20px";
				document.querySelector(".searchtip").style.overflowY="";
			}else if(resp.length<4){
				document.querySelector(".searchtip").style.height="auto";
				document.querySelector(".searchtip").style.overflowY="";
			}
			let tipsbox = document.querySelector(".searchtip");
			let html=``;
			resp.forEach(r=>{
				html+=`<div onclick="bindfuture()">`+r.sname+`</div>`
			})
			tipsbox.innerHTML=html;
		})
	})
}

function bindfuture(){
	let divname = event.currentTarget.innerText;
	location.href="/index?v=search&searchvalue="+divname;
}


function xllijdbc(){
	let xid = parseInt(event.currentTarget.getAttribute("id"));
	$.ajax({
		url:"/index?v=searchli",
		type:"post",
		data:{
			xid
		},
		success:((resp)=>{
			let r = JSON.parse(resp);
			let showNavtwoson = document.querySelector(".showNavtwoson");
			let html = ``;
			r.forEach(y=>{
				html+=`<li onclick="chuandetail(${y.sid},${y.lid})">`+y.sname+`</li>`
			})
			showNavtwoson.innerHTML=html;
		})
	})
}

function chuandetail(params1,params2){
	location.href="/productDetails?sid="+params1+"&lid="+params2;
}

function isnull(){
	location.href="/login"
}
function iscart(){
	location.href="/goods"
}
function Login(){
	let login = document.querySelector("#Login");
	let levitation = document.querySelector("#levitation");

	login.onmouseenter = function (){
		levitation.style.display = "block";
	}
	levitation.onmouseleave = function (){
		levitation.style.display = "none";
	}
}

function shield(){
	let mask = document.querySelector(".mask");
	mask.style.display = "block";

	let popup = document.querySelector(".popup");
	popup.style.display = "block";
}
function cancel_shield(){
	let mask = document.querySelector(".mask");
	mask.style.display = "none";

	let popup = document.querySelector(".popup");
	popup.style.display = "none";
}
function daohanglan() {
	let allLi = document.querySelectorAll(".gnb_nav_center li");
	let show = document.querySelector("#show");
	let zzc = document.querySelector("#zzc");
	let img = document.querySelectorAll(".showNavone li span img");
	let showli = document.querySelectorAll(".showNavone li");
	let showNavtwoson = document.querySelector(".showNavtwoson");
	allLi.forEach(x => {
		x.addEventListener("mouseover", function() {
			document.querySelector("#show").style.height = 680 + "px";
			zzc.style.display = "block";

			showli.forEach(x => {
				x.style.fontWeight = "";
			})

			for (var i = 0; i < img.length; i++) {
				if (i == 0) {
					img[i].style.display = "block";
					document.querySelector(".showNavone li").style
						.fontWeight = "bold";
				} else {
					img[i].style.display = "none";
				}
			}
			allLi.forEach(y => {
				y.style.background = "#fff";
				y.style.color = "#000";
			})
			event.target.style.background = "#000";
			event.target.style.color = "#ffff";

			showNavtwoson.style.display = "block";
		})

	})
	document.querySelector(".gnb_nav").onmouseleave = function() {
		show.style.height = 0 + "px";
		zzc.style.display = "none";
		allLi.forEach(y => {
			y.style.background = "#fff";
			y.style.color = "#000";
		})
	}

	let cancel = document.querySelector("#cancelshow");
	cancel.addEventListener("click", function() {
		show.style.height = 0 + "px";
	})

	showli.forEach(x => {
		x.addEventListener("mouseover", function() {
			img.forEach(x => {
				x.style.display = "none";
			})
			let hhh = event.currentTarget.querySelector("span img");
			hhh.style.display = "block";
			showli.forEach(y => {
				y.style.fontWeight = "";
			})
			event.currentTarget.style.fontWeight = "bold";

			showNavtwoson.style.display = "none";
			setTimeout(() => {
				showNavtwoson.style.display = "block"
			}, 150);
		})
	})
}
function detail(){
	$.ajax({
		url:"/productDetails?v=selectAll",
		type:"post",
		data:{

		},
		async:false,
		success:(resp=>{
			let select = document.querySelector(".select");
			if (resp!=null){
				for (let i = 0; i < resp.length; i++) {
					document.querySelector(".sname").innerHTML=resp[0].sname;
					document.querySelector(".sprice").innerHTML=resp[0].sprice;
					document.querySelector(".bot_type>span").innerHTML=resp[0].sname;
					document.querySelector(".tnum").innerHTML=resp[0].sprice;
					document.querySelector(".nav_box span").innerHTML=resp[0].sname;
				}
				resp.forEach(x=>{
					let mould = document.createElement("div");
					mould.className="mould";
					let div=document.createElement("div");
					select.append(mould);
					mould.append(div);
					div.innerHTML=x.sname;
					mould.addEventListener("click",function (){
						let id=x.sid;

						let aa=document.querySelectorAll(".mould");
						// let n1 = document.querySelector(".mone");
						// let n = document.querySelectorAll(".mone");

						aa.forEach(a=>{
							a.style.background="white";
							a.style.color="black";
						})
						mould.style.background=mould.style.background=="white"?"black":"white";
						mould.style.color=mould.style.color=="black"?"white":"black";

							$.ajax({
								url: "/productDetails?v=change",
								type:"post",
								data:{
									id
								},
								async:false,
								dataType: "json",
								success:(r=>{

									if (r!=null){
										let sprice = document.querySelector(".sprice");
										let sname = document.querySelector(".sname");
										let span = document.querySelector(".bot_type>span")
										let span_price = document.querySelector(".tnum");
										let span_sname= document.querySelector(".nav_box span");
										// let s = JSON.parse(r.com);
										sprice.innerHTML=r.com.xprice;
										sname.innerHTML=r.com.sname;
										span.innerHTML=r.com.sname;
										span_price.innerHTML=r.com.xprice;
										span_sname.innerHTML=r.com.sname;
									}
									$.ajax({
										url: "/productDetails?v=selectAllmemory",
										type:"post",
										data:{
											id
										},
										async:false,
										success:(r=>{
											let mone = document.querySelectorAll(".mone");
											let sprice = document.querySelector(".sprice");
											let span = document.querySelector(".bot_type>span")
											let span_price = document.querySelector(".tnum");
											console.log(r.list);
											console.log(r.nid)
											let n=r.nid;
											for (let i = 0; i < r.list.length; i++) {
												mone[i].addEventListener("click",function (){
													let nid=r.list[i].nid;
													console.log(nid)
													sprice.innerHTML=r.list[i].xprice;
													span_price.innerHTML=r.list[i].xprice;
												})
												// if (n==undefined){
												//
												// }else{
												// 	for (let i = 0; i < r.list.length; i++) {
												// 		sprice.innerHTML=r.list[n-1].xprice;
												// 		span_price.innerHTML=r.list[n-1].xprice;
												// 	}
												// }

											}
										})
									})
									$.ajax({
										url: "/productDetails?v=selectAllimg",
										type:"post",
										data:{
											id
										},
										async:false,
										success:(r=>{
											console.log(r)
											let img = document.querySelectorAll("#lunbo img");
											console.log(img)
											for (let i = 0; i < r.length; i++) {
												img[i].src=r[i].tname;
											}
										})
									})
									$.ajax({
										url: "/productDetails?v=selectAllxiaimg",
										type:"post",
										data:{
											id
										},
										async:false,
										success:(r=>{
											console.log(r)
											let img = document.querySelectorAll("#fold img");
											console.log(img)
											for (let i = 0; i < r.length; i++) {
												img[i].src=r[i].tname;
											}
										})
									})
									//商品详情页点击商品显示是否已经收藏
									$.ajax({
										url:"/productDetails?v=isLike",
										type:"post",
										data:{},
										async:false,
										success:(r=>{
											let like = document.querySelector(".like");
											if (r==1){
												like.src="img/like.png"
											}else{
												like.src="img/collect.png"
											}
										})
									})
								})
							})
						prohibit(id);
						prohibitColor(id);
					})
				})
			}
		})
	})
}

function memory(){
	$.ajax({
		url:"/productDetails?v=memory",
		type:"post",
		data:{

		},
		async:false,
		success:(resp=>{
			console.log(resp)
			let mory = document.querySelector(".mory");
			if (resp!=null){
				resp.forEach(x=>{
					let mone = document.createElement("div");
					mone.className="mone";
					let span = document.createElement("span");
					mory.append(mone);
					mone.append(span);
					span.innerHTML=x.nname;
					mone.addEventListener("click",function (){
						let nid = x.nid;
						let id=x.sid;
						let price = x.xprice;
						document.querySelector(".sprice").innerHTML=price;
						document.querySelector(".tnum").innerHTML=price;
						document.querySelector(".bot_memory").innerHTML=x.nname;
						let a =document.querySelectorAll(".mone");
						a.forEach(a=>{
							a.style.background="white";
							a.style.color="black";
						})
						mone.style.background=mone.style.background=="white"?"black":"white"
						mone.style.color=mone.style.color=="black"?"white":"black"
						$.ajax({
							url:"/productDetails?v=memorychange",
							type:"post",
							data:{
								nid,
								id
							},
							async:false,
							success:(r=>{
								// let n=JSON.parse(r);
								console.log(r)
							})

						})
						prohibit();
						// prohibitColor();
					})
				})
			}

		})
	})
}



function color(){
	$.ajax({
		url:"/productDetails?v=color",
		type:"post",
		data:{
		},
		async:false,
		success:(resp=>{
			console.log(resp)
			let col = document.querySelector(".col_span")
			let col_txt = document.querySelector(".col_txt");
			let bot_color = document.querySelector(".bot_color");
			if (resp!=null){
				let i;
				resp.forEach(x=>{
					let span = document.createElement("span")
					span.className="col_color";
						col.append(span);
					span.addEventListener("click",function (){
						let yid = x.yid;
						col_txt.innerHTML=x.yname;
						bot_color.innerHTML="|"+x.yname;
						let s = document.querySelectorAll(".col_span span");
						s.forEach(bc=>{
							bc.style.borderColor="blue";
						})
						event.currentTarget.style.borderColor="white"
						$.ajax({
							url:"/productDetails?v=colorYid",
							type:"post",
							data:{
								yid
							},
							async:false,
							success:(resp=>{

							})
						})
						// prohibit()
						prohibitColor();
					})
				})
			}
		})
	})
}

function img(){
	$.ajax({
		url:"/productDetails?v=selectimg",
		type:"post",
		data:{

		},
		async:false,
		success:(resp=>{
			console.log(resp)
			let over = document.querySelector(".over");
			let lunbo = document.querySelector("#lunbo");
			console.log(lunbo)
			resp.forEach(x=>{
				let img = document.createElement("img");
				img.src=x.tname;
				lunbo.append(img);
			})
		})
	})
}
function xiaImg() {
	$.ajax({
		url: "/productDetails?v=selectxiaimg",
		type: "post",
		data: {},
		async:false,
		success: (resp => {
			let fold = document.querySelector("#fold");
			resp.forEach(x=>{
				let img = document.createElement("img");
				img.src=x.tname;
				fold.append(img);
			})
		})
	})
}

function plus(){
	let nums = document.querySelector("#num");
	let tatil = document.querySelector(".tnum");
	let sprice = document.querySelector(".sprice").innerHTML;
	num=parseInt(nums.innerHTML)+1;
	nums.innerHTML=num;
	tatils=parseInt(sprice)*parseInt(nums.innerHTML);
	tatil.innerHTML=tatils;
}
function min(){
	let nums = document.querySelector("#num");
	let tatil = document.querySelector(".tnum");
	let sprice = document.querySelector(".sprice").innerHTML;
	if (parseInt(nums.innerHTML)==1){
		nums.innerHTML=1;
	}else{
		num=parseInt(nums.innerHTML)-1;
		nums.innerHTML=num;
	}
	tatils=parseInt(sprice)*parseInt(nums.innerHTML);
	tatil.innerHTML=tatils;
}

function insertLike(){
	$.ajax({
		url:"/productDetails?v=insertLike",
		type:"post",
		data:{

		},
		async:false,
		success:function (resp){
			let like = document.querySelector(".like");
			console.log(resp)
			if (resp=="null"){
				location.href="/login";
			}else {
				if (resp=="收藏成功"){
					like.src="img/like.png"
				}else if (resp=="取消收藏"){
					like.src="img/collect.png"
				}
			}
		}
	})
}
function islike(){
	$.ajax({
		url:"/productDetails?v=isLike",
		type:"post",
		data:{

		},
		async:false,
		success:function (resp){
			let like = document.querySelector(".like");
			if (resp=="null"){
				like.src="img/collect.png"
			}else{
				if (resp==1){
					like.src="img/like.png"
				}else{
					like.src="img/collect.png"
				}
			}
		}
	})
}

function addCart(){
	let nums = document.querySelector("#num").innerHTML;
	let sprice = document.querySelector(".sprice").innerHTML;
	let price = parseInt(sprice);
	let num=parseInt(nums);
	$.ajax({
		url:"/productDetails?v=addCart",
		type:"post",
		data:{
			price,
			num,
		},
		success:function (resp){
			console.log(resp)
			if (resp=="null"){
				location.href="/login";
			}else{
				if (resp=="加入购物车"){
					shield();
				}else if (resp=="加入失败"){
					location.href="/productDetails"
				}
			}
		}
	})
}

function jump(){
		location.href="/goods"
}

function buy(){
	let nums = document.querySelector("#num").innerHTML;
	let sprice = document.querySelector(".sprice").innerHTML;
	let price = parseInt(sprice);
	let num=parseInt(nums);
	$.ajax({
		url:"/productDetails?v=buy",
		type:"post",
		data:{
			price,
			num
		},
		success:function (resp){
			if (resp=="null"){
				location.href="/login"
			}else {
				location.href="/orderXq"
			}
		}
	})
}

function prohibit(){
	$.ajax({
		url:"/productDetails?v=prohibit",
		type:"post",
		data:{},
		async:false,
		success:function (resp){
			console.log(resp)
			let color = document.querySelectorAll(".col_color");
			let a = [1,2,3,4]
			let b = [];
			for (let i = 0; i < resp.length; i++) {
				b.push(resp[i].yid)
			}
			var d = a.filter(function(v){
				return b.indexOf(v) == -1
			})
			for (let i = 0; i < color.length; i++) {
				color[i].style.pointerEvents="auto"
				color[i].style.cursor="pointer"
			}
			d.forEach(x=>{
				if (resp.length<4){
					switch (x){
						case 1:
							color[0].style.cursor="not-allowed";
							color[0].style.pointerEvents="none";
							color[0].style.borderColor="blue"
							break;
						case 2:
							color[1].style.cursor="not-allowed";
							color[1].style.pointerEvents="none";
							color[1].style.borderColor="blue"
							break;
						case 3:
							color[2].style.cursor="not-allowed";
							color[2].style.pointerEvents="none";
							color[2].style.borderColor="blue"
							break;
						case 4:
							color[3].style.cursor="not-allowed";
							color[3].style.pointerEvents="none";
							color[3].style.borderColor="blue"
							break;
					}
				}
			})
		}
	})
}

function prohibitColor(){
	$.ajax({
		url:"/productDetails?v=prohibitColor",
		type:"post",
		data:{},
		async:false,
		success:function (resp){
			console.log(resp)
			let mone = document.querySelectorAll(".mone");
			let a = [1,2,3,4]
			let b = [];
			for (let i = 0; i < resp.length; i++) {
				b.push(resp[i].nid)
			}
			var d = a.filter(function(v){
				return b.indexOf(v) == -1
			})
			for (let i = 0; i < mone.length; i++) {
				mone[i].style.pointerEvents="auto"
				mone[i].style.cursor="pointer"
			}
			console.log(d)
			d.forEach(x=>{
				switch (d[0]){
					case 1:
						mone[0].style.cursor="not-allowed";
						mone[0].style.pointerEvents="none";
						break;
					case 2:
						mone[1].style.cursor="not-allowed";
						mone[1].style.pointerEvents="none";
						break;
					case 3:
						mone[2].style.cursor="not-allowed";
						mone[2].style.pointerEvents="none";
						break;
					case 4:
						mone[3].style.cursor="not-allowed";
						mone[3].style.pointerEvents="none";
						break;
				}
			})
		}
	})
}



